import { Axios, AxiosResponse } from 'axios'
import axios from './interceptor'

// 获取图书分类标签
function getLableData() {
  return axios.get('/article/label')
}
// 添加图书分类标签
function addLableData(labelName: string) {
  return axios.post('/article/label', {
    labelName,
  })
}
// 删除图书分类标签
function deleteLable(id: string) {
  return axios.delete(
    `/article/label?id=${id}&token=${localStorage.getItem('token')}`
  )
}
// 上传图书内的图片
function postArticleImage(imgData: FormData) {
  return axios.post('/article/image', imgData, {
    headers: {
      'Context-Type': 'multipart/form-data',
    },
  })
}

// 删除上传多余的图片
function deleteArticleImages(delImages: string[]) {
  return axios.post('/article/image/del', {
    delImages,
  })
}

// 上传图书
function postArticle(
  title: string,
  info: string,
  bg: string = '',
  articleType: string = '草稿'
) {
  return axios.post('/article', {
    title,
    info,
    articleType,
    bg,
  })
}

// 获取后台图书
function getArticleData(page: string | number, where: string) {
  return axios.get(`/article/endList?page=${page}&limite=4&where=${where}`)
}

// 删除单个图书
function deleteArticle(id: string, images: string[]) {
  return axios.post('/article/del', {
    id,
    token: localStorage.getItem('token'),
    images,
  })
}

// 获取单个图书
function getArticleSingle(id: string = '') {
  return axios.get(`/article/single?id=${id}`)
}
// 修改图书
function updateArticle(
  _id: string = '',
  title: string,
  info: string,
  articleType: string,
  bg: string
) {
  if (!_id) return { data: { ok: 0, info: '没有传入id' } }

  return axios.put('/article', {
    _id,
    title,
    info,
    articleType,
    bg,
  })
}
export {
  getLableData,
  addLableData,
  deleteLable,
  postArticleImage,
  deleteArticleImages,
  postArticle,
  getArticleData,
  deleteArticle,
  getArticleSingle,
  updateArticle,
}
